<h3 class="page-header">
  <%= page_title %> <small><%= @category.hint.presence || (@category.parent && @category.parent.children_hint) %></small>
</h3>

<div class="row">
  <div class="col-md-7">
    <%= simple_form_for(@category, url: @category.new_record? ? cip_admin_categories_path : cip_admin_category_path(@category)) do |f| %>
      <%= f.error_messages %>

      <%= f.input :title %>

      <div class="form-group file optional category_avatar">
        <label class="file optional control-label" for="page_avatar">预览图</label>
        <a class="text-muted" href="<%= @category.avatar.url(:original) %>" target="_blank">点击查看原图</a>
        <label class="clear-file pull-right" for="cip_category__destroy_avatar">
          <%= f.check_box :_destroy_avatar %>
          删除预览图
        </label>
        <%= f.file_field :avatar, style: "display: none;" %>
        <div id="cip_category_avatar_thumb" class="dropzone dz-clickable">
          <%= image_tag(@category.avatar.url(:thumb)) %>
          <span class="text-muted dz-message">点击或拖拽文件到此处更换预览图</span>
        </div>
      </div>

      <%= f.input :forwarding_type, collection: Cip::Category::FORWARDING_TYPES, as: :radio_buttons %>

      <div id="tab-page" class="cip_category_forwarding_type-tabs">
        <%= f.input :forwarding_page_id, collection: Cip::Page.all, input_html: { class: 'select2' }, label: false %>
      </div>

      <div id="tab-category" class="cip_category_forwarding_type-tabs">
        <%= f.input :forwarding_category_id, collection: Cip::Category.all, input_html: { class: 'select2' }, label: false %>
      </div>

      <div id="tab-url" class="cip_category_forwarding_type-tabs">
        <%= f.input :forwarding_url, label: false %>
      </div>

      <% if superuser? %>
        <h4>网站结构信息，请勿随意修改</h4>

        <%= f.input :slug %>
        <%= f.input :position %>
        <%= f.input :parent_id %>
        <%= f.input :editable %>
        <%= f.input :hidden %>
        <%= f.input :disabled %>
        <%= f.input :hint %>
        <%= f.input :template %>
        <%= f.input :page_hint %>
        <%= f.input :page_template %>
        <%= f.input :unparsed_page_metadata_struct, as: :text %>
        <%= f.input :children_hint %>
        <%= f.input :unparsed_children_metadata_struct, as: :text %>
      <% end %>

      <% @parent_category = @category.parent || Cip::Category.root %>
      <% if @parent_category && @parent_category.children_metadata_struct %>
        <h4>更多信息</h4>

        <%= f.fields_for :metadata, OpenStruct.new(@category.metadata) do |ff| %>
          <% @parent_category.children_metadata_struct.each do |key, options| %>
            <%= ff.input key, { required: false }.merge(options.symbolize_keys!) %>
          <% end %>
        <% end %>
      <% end %>

      <div class="pull-right">
        <%= link_to "删除", cip_admin_category_path(@category), class: "btn btn-danger", method: :delete, data: { confirm: "您确定要删除这个分类？" } if !@category.new_record? && superuser? %>
        <%= f.button :submit, "保存", class: "btn-primary" %>
      </div>
    <% end %>
  </div>
</div>

<script type="text/javascript">
  $("#cip_category_avatar").dropzone({
    preview: $("#cip_category_avatar_thumb")
  });

  var $forwarding_type_radios = $(".cip_category_forwarding_type input[type=radio]");
  var $forwarding_type_tabs = $(".cip_category_forwarding_type-tabs");

  function change_forwarding_type() {
    $forwarding_type_tabs.hide();
    var forwarding_type = $forwarding_type_radios.filter(":checked").val();
    if (forwarding_type) $("#tab-" + forwarding_type).show();
  }
  $forwarding_type_radios.change(change_forwarding_type);

  function init_change_forwarding_type() {
    if ($("#cip_category_forwarding_page_id.initialized, #cip_category_forwarding_category_id.initialized").length === 2) {
      change_forwarding_type();
    } else {
      setTimeout(init_change_forwarding_type, 100);
    }
  }

  init_change_forwarding_type();

</script>
